package stack;

public class ArrayStackDemo {

    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(3);
        arrayStack.push(6);
        arrayStack.push(7);
        arrayStack.push(2);
        arrayStack.push(9);
        arrayStack.push(7);
        arrayStack.list();
        System.out.println("-------------------------------");
        arrayStack.pop();
        System.out.println("-------------------------------");
        arrayStack.list();
    }
}

//用数组Array模拟栈
class ArrayStack{
    private int maxSize;//栈的最大深度
    private int[] stack;//栈的数据
    private int top = -1;//栈顶，当为-1时表示栈中没有元素

    public ArrayStack(int maxSize){
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //判断栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    //判断栈空
    public boolean isEmpty(){
        return top == -1;
    }

    //入栈
    public void push(int i){
        if (!isFull()){
            stack[++top] = i;
        }
    }

    //出栈
    public int pop(){
        if (!isEmpty()){
            return stack[--top];
        }
        return 0;
    }

    //遍历
    public void list(){
        if (isEmpty()){
            return;
        }
        for (int j = top; j >= 0; j--){
            System.out.println(stack[j]);
        }
    }
}